/*
This file is part of Keepass2Android, Copyright 2013 Philipp Crocoll. This file is based on Keepassdroid, Copyright Brian Pellin.

  Keepass2Android is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  Keepass2Android is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with Keepass2Android.  If not, see <http://www.gnu.org/licenses/>.
  */

using System;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Widget;
using keepass2android;
using KeePassLib;
using KeePassLib.Utility;

namespace keepass2android
{
  [Activity(Label = "@string/app_name", Theme = "@style/Kp2aTheme_Dialog")]
  public class GroupEditActivity : LifecycleAwareActivity
  {
    public const String KeyParent = "parent";
    public const String KeyName = "name";
    public const String KeyIconId = "icon_id";
    public const String KeyCustomIconId = "custom_icon_id";
    public const string KeyGroupUuid = "group_uuid";

    private ActivityDesign _design;

    private int _selectedIconId;
    private PwUuid _selectedCustomIconId = PwUuid.Zero;
    private PwGroup _groupToEdit;

    public GroupEditActivity()
    {
      _design = new ActivityDesign(this);
    }

    protected GroupEditActivity(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
    {

    }

    public const int RequestCodeGroupEdit = 9713;


    public static void Launch(Activity act, PwGroup parentGroup)
    {
      Intent i = new Intent(act, typeof(GroupEditActivity));

      PwGroup parent = parentGroup;
      i.PutExtra(KeyParent, parent.Uuid.ToHexString());

      act.StartActivityForResult(i, RequestCodeGroupEdit);
    }

    public static void Launch(Activity act, PwGroup parentGroup, PwGroup groupToEdit)
    {
      Intent i = new Intent(act, typeof(GroupEditActivity));

      PwGroup parent = parentGroup;
      i.PutExtra(KeyParent, parent.Uuid.ToHexString());
      i.PutExtra(KeyGroupUuid, groupToEdit.Uuid.ToHexString());

      act.StartActivityForResult(i, GroupEditActivity.RequestCodeGroupEdit);
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
      _design.ApplyDialogTheme();

      base.OnCreate(savedInstanceState);
      SetContentView(Resource.Layout.group_edit);

      ImageButton iconButton = (ImageButton)FindViewById(Resource.Id.icon_button);
      iconButton.Click += (sender, e) =>
      {
        IconPickerActivity.Launch(this);
      };
      _selectedIconId = (int)PwIcon.FolderOpen;

      iconButton.SetImageDrawable(App.Kp2a.CurrentDb.DrawableFactory.GetIconDrawable(this, App.Kp2a.CurrentDb.KpDatabase, (PwIcon)_selectedIconId, null, true));

      Button okButton = (Button)FindViewById(Resource.Id.ok);
      okButton.Click += (sender, e) =>
      {
        TextView nameField = (TextView)FindViewById(Resource.Id.group_name);
        String name = nameField.Text;

        if (name.Length > 0)
        {
          Intent intent = new Intent();

          intent.PutExtra(KeyName, name);
          intent.PutExtra(KeyIconId, _selectedIconId);
          if (_selectedCustomIconId != null)
            intent.PutExtra(KeyCustomIconId, MemUtil.ByteArrayToHexString(_selectedCustomIconId.UuidBytes));
          if (_groupToEdit != null)
            intent.PutExtra(KeyGroupUuid, MemUtil.ByteArrayToHexString(_groupToEdit.Uuid.UuidBytes));

          SetResult(Result.Ok, intent);

          Finish();
        }
        else
        {
          App.Kp2a.ShowMessage(this, Resource.String.error_no_name, MessageSeverity.Error);
        }
      };

      if (Intent.HasExtra(KeyGroupUuid))
      {
        string groupUuid = Intent.Extras.GetString(KeyGroupUuid);
        _groupToEdit = App.Kp2a.CurrentDb.GroupsById[new PwUuid(MemUtil.HexStringToByteArray(groupUuid))];
        _selectedIconId = (int)_groupToEdit.IconId;
        _selectedCustomIconId = _groupToEdit.CustomIconUuid;
        TextView nameField = (TextView)FindViewById(Resource.Id.group_name);
        nameField.Text = _groupToEdit.Name;
        App.Kp2a.CurrentDb.DrawableFactory.AssignDrawableTo(iconButton, this, App.Kp2a.CurrentDb.KpDatabase, _groupToEdit.IconId, _groupToEdit.CustomIconUuid, false);
        SetTitle(Resource.String.edit_group_title);
      }
      else
      {
        SetTitle(Resource.String.add_group_title);
      }



      Button cancel = (Button)FindViewById(Resource.Id.cancel);
      cancel.Click += (sender, e) =>
      {
        Intent intent = new Intent();
        SetResult(Result.Canceled, intent);

        Finish();
      };
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
      base.OnActivityResult(requestCode, resultCode, data);
      switch ((int)resultCode)
      {
        case EntryEditActivity.ResultOkIconPicker:
          _selectedIconId = data.Extras.GetInt(IconPickerActivity.KeyIconId, (int)PwIcon.Key);
          String customIconIdString = data.Extras.GetString(IconPickerActivity.KeyCustomIconId);
          if (!String.IsNullOrEmpty(customIconIdString))
            _selectedCustomIconId = new PwUuid(MemUtil.HexStringToByteArray(customIconIdString));
          else
            _selectedCustomIconId = PwUuid.Zero;

          ImageButton currIconButton = (ImageButton)FindViewById(Resource.Id.icon_button);
          App.Kp2a.CurrentDb.DrawableFactory.AssignDrawableTo(currIconButton, this, App.Kp2a.CurrentDb.KpDatabase, (PwIcon)_selectedIconId, _selectedCustomIconId, false);
          break;
      }
    }

    protected override void OnResume()
    {
      base.OnResume();
      //DON'T: _design.ReapplyTheme();
      // (This causes endless loop creating/recreating. More correct: ReapplyDialogTheme (which doesn't exist) Not required anyways...)
    }
  }
}

